// 选择图片
const chooseImage = (count) => {
	return new Promise(reslove => {
		if (count == null) {
			count = 9
		}
		uni.chooseImage({
			count: count, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera '], //从相册选择
			success: function(res) {
				reslove(res.tempFilePaths)
			},
			fail(err) {
				reslove({
					err: err
				})
			}
		});
	})
}
// 获取图片信息
//width	Number	图片宽度，单位px
//height	Number	图片高度，单位px
//path	String	返回图片的本地路径
//type	String	返回图片的格式	小程序
const getImageInfo = (src) => {
	return new Promise(reslove => {
		uni.getImageInfo({
			src: src,
			success: function(image) {
				reslove(image)
			},
			fail(err) {
				reslove({
					err: err
				})
			}
		});
	})
}
// 保存图片到系统相册
const saveImageToPhotosAlbum = (src) => {
	return new Promise(reslove => {
		uni.saveImageToPhotosAlbum({
			filePath: src,
			success: function() {
				reslove(true)
			},
			fail(err) {
				reslove({
					err: err
				})
			}
		});
	})
}
//img path to base64
const pathToBase64 = (path) => {
	return new Promise(function(resolve) {
		if (typeof window === 'object' && 'document' in window) {
			var canvas = document.createElement('canvas')
			var c2x = canvas.getContext('2d')
			var img = new Image
			img.onload = function() {
				canvas.width = img.width
				canvas.height = img.height
				c2x.drawImage(img, 0, 0)
				resolve(canvas.toDataURL())
			}
			img.onerror = reject
			img.src = path
			return
		}
		if (typeof plus === 'object') {
			
			var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
			bitmap.load(path, function() {
				try {
					var base64 = bitmap.toBase64Data()
				} catch (error) {
					reject(error)
				}
				bitmap.clear()
				resolve(base64)
			}, function(error) {
				bitmap.clear()
				reject(error)
			})
			return
		}
		if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
			wx.getFileSystemManager().readFile({
				filePath: path,
				encoding: 'base64',
				success: function(res) {
					resolve(res.data)
					// resolve('data:image/png;base64,' + res.data)
				},
				fail: function(error) {
					reject(error)
				}
			})
			return
		}
		resolve({
			err:"不支持改平台"
		})
	})
}

const base64ToPath = (base64) => {
	return new Promise(function(resolve, reject) {
		if (typeof window === 'object' && 'document' in window) {
			base64 = base64.split(',')
			var type = base64[0].match(/:(.*?);/)[1]
			var str = atob(base64[1])
			var n = str.length
			var array = new Uint8Array(n)
			while (n--) {
				array[n] = str.charCodeAt(n)
			}
			return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], {
				type: type
			})))
		}
		var extName = base64.match(/data\:\S+\/(\S+);/)
		if (extName) {
			extName = extName[1]
		} else {
			reject(new Error('base64 error'))
		}
		var fileName = Date.now() + '.' + extName
		if (typeof plus === 'object') {
			var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
			bitmap.loadBase64Data(base64, function() {
				var filePath = '_doc/uniapp_temp/' + fileName
				bitmap.save(filePath, {}, function() {
					bitmap.clear()
					resolve(filePath)
				}, function(error) {
					bitmap.clear()
					reject(error)
				})
			}, function(error) {
				bitmap.clear()
				reject(error)
			})
			return
		}
		if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
			var filePath = wx.env.USER_DATA_PATH + '/' + fileName
			wx.getFileSystemManager().writeFile({
				filePath: filePath,
				data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
				encoding: 'base64',
				success: function() {
					resolve(filePath)
				},
				fail: function(error) {
					reject(error)
				}
			})
			return
		}
		reject(new Error('not support'))
	})
}

export default {
	chooseImage,
	getImageInfo,
	saveImageToPhotosAlbum,
	pathToBase64,
	base64ToPath
}
